<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>历史告警</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">

function searchAlarmInfo(){
	var startTime=$("#i1").datebox("getValue");
	var endTime=$("#i2").datebox("getValue");
	var startTimeLong = new Date(startTime.replace(new RegExp("-","gm"),"/")).getTime();
	var endTimeLong = new Date(endTime.replace(new RegExp("-","gm"),"/")).getTime();
	
	 if(startTimeLong-endTimeLong>0){
		 $.messager.alert("系统提示","查询开始时间"+startTime+"不能大于结束时间"+endTime+"！");
		 return;
	 }

	var level = $("#alarmType1").combotree("getValues");
	 
	 $("#dg").datagrid('load',{
		"siteName":$("#siteName").combotree("getText"),
	 	"startTime":startTimeLong,
	 	"endTime":endTimeLong,
		"alarmTypeIds": level.toString()
	 });
}
	
	function exportAlarmInfoInCurrentPage(){
		var rows = $("#dg").datagrid("getRows");
		 var strIds=[];
		 for(var i=0;i<rows.length;i++){
			 strIds.push(rows[i].alarmInfoId);
		 }
		 var ids=strIds.join(",");
		 window.open('${pageContext.request.contextPath}/alarmInfo/export.do?alarmInfoIds='+ids+'&realtime=0');
	}
	
	function exportByTemplateInCurrentPage(){
		var rows = $("#dg").datagrid("getRows");
		var strIds=[];
		 for(var i=0;i<rows.length;i++){
			 strIds.push(rows[i].alarmInfoId);
		 }
		 var ids=strIds.join(",");
		 window.open('${pageContext.request.contextPath}/alarmInfo/exportByTemplate.do?alarmInfoIds='+ids+'');
	}
	
	function exportAlarmInfoInAllPages(){
		var startTime=$("#i1").datebox("getValue");
		var endTime=$("#i2").datebox("getValue");
		var startTimeLong = new Date(startTime.replace(new RegExp("-","gm"),"/")).getTime();
		var endTimeLong = new Date(endTime.replace(new RegExp("-","gm"),"/")).getTime();
		var frontId=$("#siteName").combotree("getText");
		var alarmType=$("#alarmType1").combotree("getValues");
		window.open('${pageContext.request.contextPath}/alarmInfo/exportQuery.do?frontId='+frontId+'&alarmTypeIds='+alarmType.toString()
				+'&startTime='+startTimeLong+'&endTime='+endTimeLong+'&realtime='+0);
	}
	
	function exportByTemplateInAllPages(){
		 window.open('${pageContext.request.contextPath}/alarmInfo/exportByTemplate.do?alarmInfoIds='+null+'');
	}
	
	function downloadTemplate(){
		window.open('../template/alarmInfoExportTemplate.xls');
	}
	
	function formatDatebox(value) {
        if(value ==''||value ==null){
            return;
        }else {
            var date = new Date(value);
            return date.format('yyyy-MM-dd hh:mm:ss');
        }
	}
	
	Date.prototype.format = function (fmt) {
	    var o = {
	        "M+": this.getMonth() + 1, //月份
	        "d+": this.getDate(), //日
	        "h+": this.getHours(), //小时
	        "m+": this.getMinutes(), //分
	        "s+": this.getSeconds(), //秒
	        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
	        "S": this.getMilliseconds() //毫秒
	    };
	    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	    for (var k in o)
	        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	    return fmt;
	}
	
	function formatJsonObject(value) {
		return JSON.stringify(value);
	}
	
	function formatAlarmType(value) {
		if(value==1){
			return "服务器CPU使用率告警";
		}else if(value==2){
			return "服务器内存使用率告警";
		}else if(value==3){
			return "服务器硬盘使用率告警";
		}else if(value==4){
			return "前置机故障";
		}else if(value==5){
			return "前置机掉电重启";
		}else if(value==7){
			return "前置机CPU使用率告警";
		}else if(value==8){
			return "前置机内存使用率告警";
		}else if(value==9){
			return "前置机硬盘使用率告警";
		}else if(value==10){
			return "前置机网络连接告警";
		}else if (value == 13) {
			return "外网强信号风险";
		}else if (value == 14) {
			return "外网翻频风险";
		}else if (value == 15) {
            return "上行无数据异常";
        }else if(value == 20){
            return "频谱数据入库异常告警";
        }else if(value == 21){
            return "电路域数据入库异常告警";
        }
		else{
			return value;
		}
	}
	
	function formatAlarmClass(val, row, index) {
		var alarmType = row.alarmType;
		if (alarmType == 1 || alarmType == 2 ||alarmType == 3 ||alarmType == 7 ||alarmType == 8 ||alarmType == 9) {
			return "一般告警";
		}
		if (alarmType == 4 || alarmType == 5 ||alarmType == 6 ||alarmType == 10 ||alarmType == 11 ||alarmType == 12 ||alarmType == 15||alarmType == 20 ||alarmType == 21) {
			return "紧急告警";
		}
		return "一般告警";
	}
	
	function formatIsNormal(value) {
		
		if (value == 1) {
			return '自动解除';
		} else if (value == 2) {
			return '手动解除';
		}
		return '';
	}
	
	function formatAlarmDetails(val, row, index) {
		var alarmDetails = row.alarmDetails;
		if(row.alarmType==1){
			return "服务器号:"+alarmDetails.serverId+
					" , CPU使用率:"+alarmDetails.cpuUsageRate+
					"% , CPU使用率告警门限:"+alarmDetails.cpuUsageThreshold+"%";
		}else if(row.alarmType==2){
			return "服务器号:"+alarmDetails.serverId+
					" , 内存使用率:"+alarmDetails.memoryUsageRate+
					"% , 内存使用率告警门限:"+alarmDetails.memoryUsageThreshold+"%";
		}else if(row.alarmType==3){
			return "服务器号:"+alarmDetails.serverId+
					" , 硬盘使用率:"+alarmDetails.harddiskUsageRate+
					"% , 硬盘使用率告警门限:"+alarmDetails.harddiskUsageThreshold+"%";
		}else if(row.alarmType==4){
			return "前置机号:"+alarmDetails.frontProcessorId+
					" , 前置机故障信息:"+alarmDetails.errCode
					/* +
					" , 故障时间:"+formatDatebox(alarmDetails.timestamp*1000) */
					;
		}else if(row.alarmType==5){
			return ""
			/*+"前置机号:"+alarmDetails.frontProcessorId
			 +
					" , 掉电重启时间:"+formatDatebox(alarmDetails.timestamp*1000) */
					;
		}else if(row.alarmType==15){
            return "前置机号:" + alarmDetails.frontProcessorId +
                " , 前置机故障信息:" + "上行无数据告警"
                /*+"前置机号:"+alarmDetails.frontProcessorId
                 +
                        " , 掉电重启时间:"+formatDatebox(alarmDetails.timestamp*1000) */
                ;
        }else if(row.alarmType==6){
			return "服务器号:"+alarmDetails.serverId+
					" , 丢包率:"+alarmDetails.packetLossRate+
					"% , 丢包告警门限:"+alarmDetails.packetLossRateThreshold+"%";
		}else if(row.alarmType==7){
			return "CPU内存使用率:"+alarmDetails.cpuUsageRate+
			"% , CPU使用率告警门限:"+alarmDetails.cpuUsageThreshold+"%";
		}else if(row.alarmType==8){
			return "内存使用率:"+alarmDetails.memoryUsageRate+
			"% , 内存使用率告警门限:"+alarmDetails.memoryUsageThreshold+"%";
		}else if(row.alarmType==9){
			return  "硬盘使用率:"+alarmDetails.harddiskUsageRate+
			"% , 硬盘使用率告警门限:"+alarmDetails.harddiskUsageThreshold+"%";
		}else if(row.alarmType==10){
			// return  "前置机网络连接告警";
            return formatJsonObject(row.alarmDetails);
		}else{
			return formatJsonObject(row.alarmDetails);
		}
	}

	function formatFrontProcessorId(val, row, index) {
		var alarmDetails = row.alarmDetails;
		return alarmDetails.frontProcessorId;
	}
	
	function formatIsHandle(value) {
		
		if(value==0){
			return "<font color=red>否</font>";
		}else {
			return "是";
		}
	}

	
	function formatAlarmTime(val, row, index) {
		
		// var alarmDetails = row.alarmDetails;
		// var detailsTimestamp = alarmDetails.timestamp;
		//
		// if (detailsTimestamp === undefined) {
		// 	return formatDatebox(val);
		// }
		// return formatDatebox(detailsTimestamp * (row.alarmType == 10 ? 1 : 1000));
        var alarmDetails = row.alarmDetails;
        if(!alarmDetails.timestamp){

            return formatDatebox(val);
        }else{
            //if(row.alarmType nk== 10){
            //return formatDatebox(alarmDetails.timestamp);
            //}
            return formatDatebox(val);
        }
	}
	
	function formatEndTime(val, row, index) {
		var alarmDetails = row.endTime;
		return row.endTime ? formatDatebox(val) : '';
	}
	
	formatterDate = function (date) {
		var day = date.getDate();
		var month = (date.getMonth() + 1);
		var hor = date.getHours();
		var min = date.getMinutes();
		var sec = date.getSeconds();
		return date.getFullYear() + '-' + month + '-' + day+" "+hor+":"+min+":"+sec;
	};

	$(function(){
		
		//$('#i1').datetimebox('setValue', formatterDate(new Date(new Date(new Date().toLocaleDateString()).getTime())));
		//$('#i2').datetimebox('setValue', formatterDate(new Date()));
		
		// $('#dg').datagrid({
		// 	rowStyler:function(index,row){
		// 		var alarmType = row.alarmType;
		//
		// 		if (alarmType == 4 || alarmType == 5 || alarmType == 6 || alarmType == 10 || alarmType == 11||alarmType == 15) {
		// 			return 'background-color:pink;';
		// 		}
		// 	}
		// });
		
		
		$('#alarmType1').combotree('loadData', [
 			{id:'-1', text:'一般告警',  children: [
 		    	{id: 1, text: '服务器CPU使用率告警'},
 		    	{id: 2, text: '服务器内存使用率告警'},
 		    	{id: 3, text: '服务器硬盘使用率告警'},
 		    	{id: 7, text: '前置机CPU使用率告警'},
 		    	{id: 8, text: '前置机内存使用率告警'},
 		    	{id: 9, text: '前置机硬盘使用率告警'}]},
 		    {id:'-2', text:'紧急告警',  children: [
                    {id: 4, text: '前置机故障或上行无数据告警'},
                    {id: 5, text: '前置机掉电重启'},
                    {id: 10, text: '前置机网络连接告警'},
                    {id: 20, text: '频谱数据入库异常告警'},
                    {id: 21, text: '电路域数据入库异常告警'}
   		    	]}
 		]);
		$('#alarmType1').combotree('setValues', [1,2,3,4,5,6,7,8,9,10,20,21]);
		searchAlarmInfo();
	});
</script>
</head>
<body style="margin: 1px">
<table id="dg" title="告警历史查询" class="easyui-datagrid" fitColumns="fasle" pagination="true" rownumbers="true" pageSize="100" pageList="[100,200,300,400,500]"
   url="${pageContext.request.contextPath}/alarmInfo/list.do" fit="true" toolbar="#tb">
   <thead>
   	<tr>
   		<th field="cb" checkbox="true" align="center"></th>
   		<th field="alarmInfoId" width="100" align="center">告警信息ID</th>
   		<th field="alarmType1" width="100" align="center" formatter="formatAlarmClass">告警级别</th>
   		<th field="alarmObjectType" width="120" align="center">告警对象类型</th>
   		<th field="alarmObjectInfo" width="150" align="center">告警对象标识</th>
		<th field="alarmType" width="200" align="center" formatter="formatAlarmType">告警原因</th>
		<th field="alarmTime" width="200" align="center" formatter="formatAlarmTime">首次发生时间</th>
		<th field="lastTime" width="100" align="center" formatter="formatAlarmTime">最近发生时间</th>
		<th field="count" width="100" align="center">发生次数</th>
		<th field="endTime" width="100" align="center" formatter="formatAlarmTime">清除时间</th>
		<th field="comfirmTime" width="100" align="center" formatter="formatAlarmTime">确认时间</th>
		<th field="comfirmer" width="100" align="center" >确认人</th>
		<th field="isHandle" hidden="hiden" width="100" align="center" formatter="formatIsHandle">是否已处理</th>
		<th field="isNormal" hidden="hiden" width="100" align="center" formatter="formatIsNormal">解除方式</th>
		<th field="alarmDetails" width="500" align="center" formatter="formatAlarmDetails">备注</th>
   	</tr>
   </thead>
 </table>
 <div id="tb">
 	<div>
 		<a href="javascript:exportAlarmInfoInCurrentPage()" class="easyui-linkbutton" iconCls="icon-save" plain="true">导出当前页告警信息</a>
 		<a href="javascript:exportAlarmInfoInAllPages()" class="easyui-linkbutton" iconCls="icon-save" plain="true">导出所有页告警信息</a>
 		<%--<a href="javascript:exportByTemplateInCurrentPage()" class="easyui-linkbutton" iconCls="icon-save" plain="true">用模版导出当前告警信息</a>--%>
 		<%--<a href="javascript:exportByTemplateInAllPages()" class="easyui-linkbutton" iconCls="icon-save" plain="true">用模版导出所有告警信息</a>--%>
 	</div>
 	<div>
 		&nbsp;告警对象：&nbsp;
 		<input class="easyui-combotree" id="siteName" name="siteName" style="width:180px" data-options="panelHeight:'auto', panelMaxHeight:'240px',url:'${pageContext.request.contextPath}/alarmInfo/frontBaseName.do'" multiple/>   
 		&nbsp;时间：&nbsp;<input type="text" id="i1" size="20" class="easyui-datetimebox" style="width:150px" />
 		&nbsp;至&nbsp;
		<input type="text" id="i2" size="20" class="easyui-datetimebox"  style="width:150px" />
		&nbsp;&nbsp;&nbsp;告警原因：
		<select id="alarmType1" class="easyui-combotree" style="width:220px;" multiple></select>
 		<a href="javascript:searchAlarmInfo()" class="easyui-linkbutton" iconCls="icon-search" plain="true"><b style="font-size:15px">搜索</b></a>
 	</div>
 </div>
 
</body>
</html>